#ifndef _PART_GROUP_CACHE_H_
#define _PART_GROUP_CACHE_H_

#include <vector>
#include <string>

namespace part2d
{
class Part2D;
class PartGroup;
class P2DForce;

class PartGroupCache
{
public:
	enum CACHE_FLAGS {
		CF_COPYIMAGE	= 0x00000001,
		CF_COPYFORCES	= 0x00000002,
		CF_COPYCOLORS	= 0x00000004,
		CF_COPYMAXC		= 0x00000008,
		CF_COPYONDIE	= 0x00000010,
		CF_DEACTIVATE	= 0x00000020,
		CF_COPYSYS		= 0x00000040,
		CF_ALL			= 0xFFFFFFFF,
	};

	struct CACHE_DEFAULTS 
	{
		std::string		Force;
		int				MaxC;
	};

	PartGroupCache(Part2D* pSys, CACHE_DEFAULTS* pDefault = NULL, int CacheSize = 1000);
	virtual ~PartGroupCache();

	void				Clear();
	void				Init(Part2D* pNewSystem = NULL);

	PartGroup*			GetFromCache(Part2D* pRefSys = NULL, PartGroup* pRefCopy = NULL, unsigned long flags = CF_ALL);
	bool				Release(PartGroup* pGroup);
	void				Reset(PartGroup* pGroup);

protected:
	struct CACHE_BLOCK {
		PartGroup**		pGroupsA;
		long*			AllocA;
		long			AllocSize;
	};

	CACHE_BLOCK*		Alloc();
	void				CopyGroup(PartGroup* pGroup, const PartGroup* pRef, unsigned long flags);

private:
	Part2D*				m_pSys;
	int					m_CacheSize;
	int					m_SearchIdx;
	int					m_SearchGroup;

	CACHE_DEFAULTS		m_Defaults;
	bool				m_DefaultEnabled;

	std::vector<CACHE_BLOCK*>	m_Cache;
};

}  //namespace part2d

#endif //_PART_GROUP_CACHE_H_
